package com.yuneec.fly.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuneec.droneservice.model.ControllerLocation;
import com.yuneec.fly.GlobalParams;
import com.yuneec.fly.R;
import com.yuneec.fly.activities.SystemSettingActivity;
import com.yuneec.fly.dialogs.BaseDialogFragment;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.events.commandEvent.ChangeStatusEvent;
import com.yuneec.fly.events.uiEvent.DroneDisconnectedEvent;
import com.yuneec.fly.events.uiEvent.DroneReconnectedEvent;
import com.yuneec.fly.models.DroneTypeFactory;
import com.yuneec.fly.presenters.ConstantPresenter;
import com.yuneec.fly.utils.DialogHelper;
import com.yuneec.fly.utils.LocationUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;

public class BasicInfoFragment extends BaseFragment {

    private TextView statusTV;
    private TextView heightTV;
    private TextView vSpeedTV;
    private TextView hSpeedTV;
    private TextView distanceTV;
    private ImageView droneSignalIV;
    private ImageView controllerSignalIV;
    private ImageView batteryIV;
    private TextView batteryTV;

    private String preStatus;
    private DecimalFormat decimalFormat = new DecimalFormat("#0.0");

    @Override
    protected int getContentViewResourceId() {
        return R.layout.basic_info;
    }

    @Override
    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        batteryTV = (TextView) selfView.findViewById(R.id.batteryTV);
        batteryIV = (ImageView) selfView.findViewById(R.id.batteryIV);
        controllerSignalIV = (ImageView) selfView.findViewById(R.id.controllerSignalIV);
        droneSignalIV = (ImageView) selfView.findViewById(R.id.droneSignalIV);
        distanceTV = (TextView) selfView.findViewById(R.id.distanceTV);
        hSpeedTV = (TextView) selfView.findViewById(R.id.hSpeedTV);
        vSpeedTV = (TextView) selfView.findViewById(R.id.vSpeedTV);
        heightTV = (TextView) selfView.findViewById(R.id.heightTV);
        statusTV = (TextView) selfView.findViewById(R.id.statusTV);
        selfView.findViewById(R.id.settingBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (false && GlobalParams.droneConnected) {
                    DialogHelper.showConfirmDialog(getFragmentManager(), getString(R.string.open_setting_confirm_message), new BaseDialogFragment.OnConfirmListener() {
                        @Override
                        public void onConfirm(BaseDialogFragment dialogFragment) {
                            dialogFragment.dismiss();
                            startActivity(new Intent(getActivity(), SystemSettingActivity.class));
                        }
                    });
                } else {
                    startActivity(new Intent(getActivity(), SystemSettingActivity.class));
                }
            }
        });
    }

    @Override
    public void onStart() {
        EventBusFactory.getFeedBackBus().register(this);
        EventBusFactory.getUiBus().register(this);
        super.onStart();
    }

    @Override
    public void onStop() {
        EventBusFactory.getFeedBackBus().unregister(this);
        EventBusFactory.getUiBus().unregister(this);
        super.onStop();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onStatusChange(ChangeStatusEvent event) {
        statusTV.setText(event.status);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDroneReconnected(DroneReconnectedEvent event) {
        statusTV.setText(preStatus);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDroneDisconnected(DroneDisconnectedEvent event) {
        preStatus = statusTV.getText().toString();
        statusTV.setText(getString(R.string.status_disconnect));
    }

    public void updateDroneFeedback() {
        int gpsLevel = GlobalParams.droneFeedback.satellitesNum * 15;
        droneSignalIV.setImageLevel(gpsLevel);
        if (GlobalParams.droneType == null) {
            GlobalParams.droneType = DroneTypeFactory.getDrone(GlobalParams.droneFeedback.vehicleType);
        }
        int batteryLevel = GlobalParams.droneType.getVoltageLevel(GlobalParams.droneFeedback.voltage);
        batteryIV.setImageLevel(batteryLevel);
        batteryTV.setText(decimalFormat.format(GlobalParams.droneFeedback.voltage) + "V");

        heightTV.setText("H:" + LocationUtils.formatLengthDisplayString(getActivity(), GlobalParams.droneFeedback.altitude));
        hSpeedTV.setText("H.S:" + LocationUtils.formatVelocityDisplayString(getActivity(), GlobalParams.droneFeedback.hSpeed));
        vSpeedTV.setText("V.S:" + LocationUtils.formatVelocityDisplayString(getActivity(), GlobalParams.droneFeedback.vSpeed));
        ControllerLocation location  = ConstantPresenter.getInstance().getControllerLocation();
        if (location != null && GlobalParams.droneFeedback != null
                && location.latitude != 0 && location.longitude != 0
                && GlobalParams.droneFeedback.latitude != 0 && GlobalParams.droneFeedback.longitude != 0) {
            float[] distanceBearing = LocationUtils.calculateDistanceAndBearing(GlobalParams.droneFeedback.latitude, GlobalParams.droneFeedback.longitude, GlobalParams.droneFeedback.altitude, location.latitude, location.longitude, 0.0f);
            if (distanceBearing[0] > -10) {
                distanceTV.setText("D:" + LocationUtils.formatHomeDistanceDisplayString(getActivity(), distanceBearing[0]));
            }
        }
    }

    public void updateControllerLocation() {
        ControllerLocation location  = ConstantPresenter.getInstance().getControllerLocation();
        if (location != null) {
            int level = location.satelliteCount * 15;
            controllerSignalIV.setImageLevel(level);
        }
    }
}
